package redis.session ;

import java.io.IOException ;

import javax.annotation.Resource ;
import javax.servlet.Filter ;
import javax.servlet.FilterChain ;
import javax.servlet.ServletException ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;

import org.springframework.util.StringUtils ;
import org.springframework.web.filter.OncePerRequestFilter ;

import redis.RedisConfig ;
import utils.CookieUtil ;

/**
 * 分布式session拦截器
 * @author 80002165 @date 2017年7月5日 下午8:01:37
 */
public class DistributeSessionFilter extends OncePerRequestFilter implements Filter {
    
    @Resource
    SessionService sessionService ;
    
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String sid = CookieUtil.getCookieValue(request, RedisConfig.SESSION_DISTRIBUTED_SESSIONID) ;
        if (!StringUtils.hasText(sid)) {
            sid = utils.StringUtil.getUUID() ;
            CookieUtil.setCookie(response, RedisConfig.SESSION_DISTRIBUTED_SESSIONID, sid, RedisConfig.SESSION_DISTRIBUTED_TIMEOUT) ;
        }
        HttpServletRequestWrapper requestSelf = new HttpServletRequestWrapper(sid, request, response) ;
        filterChain.doFilter(requestSelf, response) ;
    }
}
